<?php

class user_messageModel extends Model {
    
    const USER_MESSAGE_CACHE_KEY = 'wf_user_message_type_cache_';
    
    public function getRemoveMessage($uid, $type) {
        $key = self::USER_MESSAGE_CACHE_KEY.$type.'_'.$uid;
        $list = Cache_Memcache::getInstance()->get($key);
        if($list!==false) {
            return $list;
        }
        $ids = [];
        $list = $this->getAll('type=? and uid=?', [$type, $uid]);
        if($list) {
            foreach ($list as $row) {
                $ids[] = $row['message_id'];
            }
        }
        Cache_Memcache::getInstance()->set($key, $ids);
        return $ids;
    }
    
    public function removeMessage($uid, $type, $message_ids) {
        $key = self::USER_MESSAGE_CACHE_KEY.$type.'_'.$uid;
        $now = date('Y-m-d H:i:s');
        $sql = 'insert into wf_user_message (uid, message_id, type, add_time) values ';
        $sql_data = [];
        foreach ($message_ids as $id) {
            $sql_data[] = '('.$uid.','.$id.','.$type.',"'.$now.'")';
        }
        $sql .= implode(',', $sql_data);
        Cache_Memcache::getInstance()->delete($key);
        return $this->execute($sql);
    }
}